#include "stdafx.h"
#include "mt4dll.h"
#include <windows.h>


typedef int (__stdcall *SendFunc)(MqlStr * msg, int len) ;
typedef int (__stdcall *ServerInitFunc)(unsigned len, BOOL);
typedef void (__stdcall *ServerUnInitFunc)();

typedef int (__stdcall *ReceiveFunc)(MqlStr * msg, int len) ;
typedef int (__stdcall *ClientInitFunc)(BOOL);
typedef void (__stdcall *ClientUnInitFunc)();

int _tmain(int argc, _TCHAR* argv[])
{
	HMODULE hM;
	ServerInitFunc serverInit;
	SendFunc send;
	ServerUnInitFunc serverUnInit;

	ClientInitFunc clientInit;
	ReceiveFunc receive;
	ClientUnInitFunc clientUnInit;

	int ret;

	MqlStr msg[4] = {{64,"a"},
						{64,"b"},
						{64,"c"},
						{64,"d"}};

	hM = LoadLibrary((LPCSTR)"mt4dll.dll");

	serverInit = (ServerInitFunc)GetProcAddress(hM, "ServerInit");
	send = (SendFunc)GetProcAddress(hM, "Send");
	serverUnInit = (ServerUnInitFunc)GetProcAddress(hM, "ServerUnInit");

	clientInit = (ClientInitFunc)GetProcAddress(hM, "ClientInit");
	receive = (ReceiveFunc)GetProcAddress(hM, "Receive");
	clientUnInit = (ClientUnInitFunc)GetProcAddress(hM, "ClientUnInit");

#if 0
	ret = serverInit(64,1);
	
	MqlStr sndmsg;
	char buf[512];
	sndmsg.len = 512;
	sndmsg.string = buf;
	for(int i=0; i< 80; i++){
		sprintf(sndmsg.string, "msg %d:---------------", i);
		ret = send(&sndmsg, 1);
		Sleep(1000);
	}
	serverUnInit();
#else
	MqlStr recvmsg[8];


	for(int i=0;i<8;i++){
		recvmsg[i].len = 512;
		recvmsg[i].string = (char *)malloc(512);
	}

	ret = clientInit(1);
	int i=0;
	while(1){
		ret = receive(recvmsg, 8);
		if(ret){
			Sleep(1000);
		}
		i++;
	}
	clientUnInit();
	
#endif

	return 0;
}

